<?php defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * Created by PhpStorm.
 * User: fyk
 * Date: 2019/12/4
 * Time: 9:56
 */


class Customer extends Admin_Controller
{
    function __construct()
    {
        parent::__construct();
        $this->load->library('pagination');
        $this->load->model('customer_m');
    }

    /**
     * 首页列表
     * @param int $page
     */
    public function index($page = 1)
    {
        $get    = $this->input->get();
        $search = $where = [];
        if (isset($get['customer_title']) && !empty($get['customer_title'])) {
            $search['keyword'] = $keyword = $get['customer_title'];
            $where             = "name like '%{$keyword}%'";
        }
        //分页
        $limit                      = 20;
        $config['uri_segment']      = 4;
        $config['use_page_numbers'] = true;
        $config['total_rows']       = $this->customer_m->total($where);
        $config['base_url']         = base_url('manage/customer/index');
        $config['per_page']         = $limit;
        $config['num_links']        = 1;
        $start                      = ($page - 1) * $limit;
        $this->pagination->initialize($config);
        $data['pagination'] = $this->pagination->create_links();
        $customer           = $this->customer_m->select($where, $limit, $start);

        $data['search']   = $search;
        $data['customer'] = $customer;
       // pp($data);die;
        $this->load->view('customer/index', $data);
    }

    /**
     * 新增视图
     */
    public function add()
    {
        $data['pic_path'] = "[]";
        $data['pic_con']  = json_encode([]);

        $this->load->view('customer/form', $data);
    }

    /**
     * 查询修改内容
     * @param null $customer_id
     */
    public function edit($customer_id = null)
    {
        $customer = $this->customer_m->find($customer_id);
        //pp($customer);die;
        if (!$customer) {
            show_message('参数错误！', site_url(X_URL . 'customer/index'));
        }

        $del_url = base_url("index.php/Upload/delete_img?url=" . $customer['img']);;
        $pic_con[]          = [
            'caption' => "个人图",
            'size'    => 576237,
            'width'   => '120px',
            'url'     => $del_url,
            "key"     => $customer['id'],
        ];
        $data['pic_path']   = "['" . $customer['img'] . "']";
        $data['pic_con']    = json_encode($pic_con);
        $data['customer']    = $customer;
        $data['customer_id'] = $customer['id'];

        $this->load->view('customer/form', $data);
    }

    /**
     * 删除
     * @param $customer_id
     */
    public function del($customer_id)
    {
        if ($customer_id) {
            if ($this->customer_m->delete_by(['id' => $customer_id])) {
                show_message('恭喜，删除成功', site_url(X_URL . 'customer/index'), 1);
            } else {
                show_message('参数错误', site_url(X_URL . 'customer/index'));
            }
        } else {
            show_message('参数错误', site_url(X_URL . 'customer/index'));
        }
    }

    /**
     * 新增、修改
     */
    public function update()
    {

        $post          = $this->input->post();
        
        $customer_title = isset($post['title']) ? htmlspecialchars($post['title'], ENT_QUOTES) : false;
        $customer_id    = isset($post['customer_id']) ? intval($post['customer_id']) : false;
        $customer       = [
            'title'    => $customer_title,
            'name' => $post['name'] ?? 0,
            'email'   => $post['email'] ?? "",
            'img'      => $post['img'] ?? "",
            'content'  => $post['content'] ?? "",
            'tel'  => $post['tel'] ?? "",
            'israel'  => $post['israel'] ?? "",
        ];
        if (!$customer_id) {
            // 新增
            $customer['create_time'] = date("Y-m-d H:i:s", time());
            $customer               = $this->customer_m->insert($customer);
            if ($customer) {
                show_message('新增客服成功', site_url(X_URL . 'customer/index'), 1);
            } else {
                show_message('新增客服失败', site_url(X_URL . 'customer/add'));
            }
        } else {
            // 修改客服信息
            $affected_rows = $this->customer_m->update(['id' => $customer_id], $customer);
            if ($affected_rows) {
                show_message('修改客服成功', site_url(X_URL . 'customer/index'), 1);
            } else {
                show_message('修改客服失败', site_url(X_URL . 'customer/edit'));
            }
        }
    }


}


